import 'dart:convert';

import 'package:hm_show_flutter/entity/company.dart';
import 'package:hm_show_flutter/entity/init_entity.dart';
import 'package:hm_show_flutter/entity/user.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:hm_show_flutter/constant/string.dart';

class SharedPreferencesUtils {
  static String token = "";
  static const String SP_USER = "sp_user";
  static const String SP_COMPANY = "sp_company";
  static const String SP_INIT = "sp_init";

  static Future getToken() async {
    if (token == null || token.isEmpty) {
      SharedPreferences sharedPreferences =
          await SharedPreferences.getInstance();
      token = sharedPreferences.getString(Strings.token) ?? null;
    }
    return token;
  }

  static Future getImageHead() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.get(Strings.HEAD_URL);
  }

  static Future getUserName() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.get(Strings.NICK_NAME);
  }

  static Future saveUser(UserModel user) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    Map<String,dynamic> map = user.toJson();
    String str = json.encode(map);
    await sp.setString(SP_USER, str);
  }

  static Future getUser() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    String jsonUser = sp.get(SP_USER);

    if (jsonUser == null || jsonUser.isEmpty) {
      return null;
    }
    UserModel user;
    try {
      user = UserModel.fromJson(json.decode(jsonUser));
    } on Exception catch (e, s) {
      print(s);
    }
    return user;
  }

  static Future saveInit(InitData init) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    Map<String,dynamic> map = init.toJson();
    String str = json.encode(map);
    await sp.setString(SP_INIT, str);
  }

  static Future getInit() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    String jsonSp = sp.get(SP_INIT);

    if (jsonSp == null || jsonSp.isEmpty) {
      return null;
    }
    InitData user;
    try {
      user = InitData.fromJson(json.decode(jsonSp));
    } on Exception catch (e, s) {
      print(s);
    }
    return user;
  }

  static Future saveCompany(CompanyModel model) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    Map<String,dynamic> map = model.toJson();
    String str = json.encode(map);
    await sp.setString(SP_COMPANY, str);
  }

  static Future<CompanyModel> getCompany() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    String jsonUser = sp.get(SP_COMPANY);
    if (jsonUser == null || jsonUser.isEmpty) {
      return CompanyModel();
    }
    CompanyModel model;
    try {
      model = CompanyModel.fromJson(json.decode(jsonUser));
    } on Exception catch (e, s) {
      print(s);
    }
    return model;
  }
}
